package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import org.asiainfo.common.core.constant.TenantConstants;
import org.asiainfo.common.idempotent.annotation.RepeatSubmit;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.asiainfo.common.tenant.utils.TenantHelper;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysTenantPackageService;
import org.asiainfo.system.domain.vo.SysTenantPackageVo;
import org.asiainfo.system.domain.bo.SysTenantPackageBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 租户套餐表
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tenantPackage")
public class SysTenantPackageController extends BaseController {

    private final SysTenantPackageService sysTenantPackageService;

    /**
     * 分页查询
     *
     * @param sysTenantPackageBo 筛选条件
     * @param pageQuery          分页对象
     * @return 查询结果
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:list")
    @GetMapping("/list")
    public TableDataInfo<SysTenantPackageVo> list(SysTenantPackageBo sysTenantPackageBo, PageQuery pageQuery) {
        return sysTenantPackageService.queryPageList(sysTenantPackageBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param packageId 主键
     * @return 单条数据
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:query")
    @GetMapping("/{packageId}")
    public R<SysTenantPackageVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("packageId") Long packageId) {
        return R.ok(sysTenantPackageService.queryById(packageId));
    }

    /**
     * 新增数据
     *
     * @param sysTenantPackageBo 实体
     * @return 新增结果
     */
    @SaCheckPermission("system:tenantPackage:add")
    @Log(title = "租户套餐表", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTenantPackageBo sysTenantPackageBo) {
        return toAjax(sysTenantPackageService.insertByBo(sysTenantPackageBo));
    }

    /**
     * 编辑数据
     *
     * @param sysTenantPackageBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("system:tenantPackage:edit")
    @Log(title = "租户套餐表", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTenantPackageBo sysTenantPackageBo) {
        return toAjax(sysTenantPackageService.updateByBo(sysTenantPackageBo));
    }

    /**
     * 删除数据
     *
     * @param packageIds 主键
     * @return 删除是否成功
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:remove")
    @Log(title = "租户套餐表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{packageIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] packageIds) {
        return toAjax(sysTenantPackageService.deleteWithValidByIds(List.of(packageIds)));
    }

    /**
     * 修改套餐状态
     *
     * @param sysTenantPackageBo 套餐信息
     * @return 修改结果
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:edit")
    @Log(title = "租户套餐表", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysTenantPackageBo sysTenantPackageBo) {
        return toAjax(sysTenantPackageService.updatePackageStatus(sysTenantPackageBo));
    }

    /**
     * 查询租户套餐下拉列表
     *
     * @return 租户套餐
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:list")
    @GetMapping("/selectList")
    public R<List<SysTenantPackageVo>> selectList() {
        return R.ok(TenantHelper.ignore(sysTenantPackageService::selectList));
    }
}

